package com.csu.ourpetstore1.service;

//import com.sun.org.apache.xml.internal.resolver.Catalog;

import com.csu.ourpetstore1.domain.Category;
import com.csu.ourpetstore1.domain.Item;
import com.csu.ourpetstore1.domain.Product;
import com.csu.ourpetstore1.persistence.CategoryMapper;
import com.csu.ourpetstore1.persistence.ItemMapper;
import com.csu.ourpetstore1.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CatalogService
{
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ItemMapper itemMapper;

    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }

    public Category getCategory(String categoryId) {
        return categoryMapper.getCategory(categoryId);
    }

    public Product getProduct(String productId) {
        return productMapper.getProduct(productId);
    }

    public List<Product> getProductListByCategory(String categoryId) {
        return productMapper.getProductListByCategory(categoryId);
    }

    // TODO enable using more than one keyword
    public List<Product> searchProductList(String keyword) {
        return productMapper.searchProductList("%" + keyword.toLowerCase() + "%");
    }

    public List<Item> getItemListByProduct(String productId) {
        return itemMapper.getItemListByProduct(productId);
    }

    public Item getItem(String itemId) {
        return itemMapper.getItem(itemId);
    }

    public int isItemInStock(String itemId) {
        return itemMapper.getInventoryQuantity(itemId);
    }
    //    //category部分
//
//    public void insertCategory(Category category){
//        categoryMapper.insertCategory(category);
//    }
//
//    private void updateCategory(String categoryId){
//        categoryMapper.updateCategory(categoryId);
//    }
//
//    private void deleteCategory(String categoryId){
//        categoryMapper.deleteCategory(categoryId);
//    }

    //item部分
    public void insertItem(Item item){
        itemMapper.insertItem(item);
    }

    public void updateItem(Item item,String itemId){
        itemMapper.updateItem(itemId,item);
    }

    public void deleteItem(String itemId){
        itemMapper.deleteItemByIId(itemId);
    }

    //product部分
    public void insertProduct(Product product){
        productMapper.insertProduct(product);
    }

    public void updateProduct(Product product,String productId){
        productMapper.updateProduct(product,productId);
        if(product.getProductId()!=productId){
            itemMapper.updatePId(productId,product.getProductId());
        }
    }

    public void deleteProduct(String productId){
        productMapper.deleteProduct(productId);
        itemMapper.deleteItemByPId(productId);
    }

}
